<template>
  <view class="friends-page">
    <!-- 页面标题 -->
    <view class="page-header">
      <text class="page-title">密友管理</text>
    </view>
    
    <!-- 添加密友 -->
    <view class="add-friend-section">
      <view class="add-friend-card">
        <text class="section-title">添加新密友</text>
        <view class="add-form">
          <input 
            type="text" 
            placeholder="输入好友ID添加密友" 
            v-model="friendId" 
            class="friend-id-input"
          />
          <button class="add-btn" @click="addFriend">
            <text class="add-icon">+</text>
            添加
          </button>
        </view>
      </view>
    </view>

    <!-- 密友列表 -->
    <view class="friends-section">
      <text class="section-title">{{ friends.length > 0 ? `我的密友 (${friends.length})` : '我的密友' }}</text>
      <view class="friends-list">
        <view v-if="friends.length === 0" class="empty-tip">
          <text class="empty-icon">👥</text>
          <text class="empty-text">暂无密友，添加一个吧~</text>
        </view>
        <view 
          v-for="(friend, index) in friends" 
          :key="index" 
          class="friend-item"
        >
          <view class="friend-info">
            <view class="avatar-container">
              <text class="avatar">{{ friend.avatar || '👤' }}</text>
            </view>
            <view class="friend-details">
              <text class="friend-name">{{ friend.nickname }}</text>
              <text class="friend-id">ID: {{ friend.friendId }}</text>
              <view class="permission-status">
                <text 
                  class="permission-dot"
                  :class="{ enabled: friend.permissionsEnabled }"
                ></text>
                <text class="permission-text">
                  {{ friend.permissionsEnabled ? '可查看心情' : '不可查看心情' }}
                </text>
              </view>
            </view>
          </view>
          <view class="friend-actions">
            <button 
              class="action-btn view-btn" 
              @click="viewFriendStats(friend)"
              :disabled="!friend.permissionsEnabled"
            >
              <text class="btn-icon">📊</text>
              心情统计
            </button>
            <button class="action-btn delete-btn" @click="deleteFriend(index)">
              <text class="btn-icon">🗑️</text>
              删除
            </button>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { getFriends, saveFriends, getStorage } from '@/utils/storage'

export default {
  data() {
    return {
      friends: [],
      friendId: ''
    }
  },
  onLoad() {
    this.loadFriends()
  },
  methods: {
    loadFriends() {
      this.friends = getFriends()
    },
    
    addFriend() {
      if (!this.friendId.trim()) {
        uni.showToast({ title: '请输入好友ID', icon: 'none' })
        return
      }
      
      // 模拟获取好友信息（实际项目中应该从服务器获取）
      const mockFriend = {
        friendId: this.friendId.trim(),
        nickname: `用户${this.friendId.slice(-4)}`,
        avatar: '',
        permissionsEnabled: true
      }
      
      // 检查是否已添加
      const isAlreadyAdded = this.friends.some(friend => friend.friendId === mockFriend.friendId)
      if (isAlreadyAdded) {
        uni.showToast({ title: '该好友已添加', icon: 'none' })
        return
      }
      
      this.friends.push(mockFriend)
      saveFriends(this.friends)
      this.friendId = ''
      uni.showToast({ title: '添加成功' })
    },
    
    deleteFriend(index) {
      uni.showModal({
        title: '确认删除',
        content: '确定要删除这个密友吗？',
        success: (res) => {
          if (res.confirm) {
            this.friends.splice(index, 1)
            saveFriends(this.friends)
            uni.showToast({ title: '已删除' })
          }
        }
      })
    },
    
    viewFriendStats(friend) {
      // 跳转到查看密友心情统计页面
      uni.navigateTo({
        url: `/pages/user/friendStats?friendId=${friend.friendId}&nickname=${friend.nickname}`
      })
    }
  }
}
</script>

<style scoped>
.friends-page {
  padding: 20rpx;
  min-height: 100vh;
  background: linear-gradient(135deg, #e3f2fd, #bbdefb);
}

/* 页面标题 */
.page-header {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10rpx);
  text-align: center;
}

.page-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
}

/* 添加密友 */
.add-friend-section {
  margin-bottom: 30rpx;
}

.add-friend-card {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 25rpx;
  padding: 30rpx;
  box-shadow: 0 8rpx 30rpx rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(10rpx);
}

.section-title {
  font-size: 28rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 25rpx;
  display: block;
}

.add-form {
  display: flex;
  gap: 15rpx;
  align-items: center;
}

.friend-id-input {
  flex: 1;
  padding: 25rpx 30rpx;
  border: 2rpx solid rgba(33, 150, 243, 0.3);
  border-radius: 50rpx;
  font-size: 28rpx;
  background: rgba(255, 255, 255, 0.8);
  transition: all 0.3s ease;
}

.friend-id-input:focus {
  outline: none;
  border-color: rgba(33, 150, 243, 0.8);
  box-shadow: 0 0 0 5rpx rgba(33, 150, 243, 0.1);
}

.add-btn {
  background: linear-gradient(135deg, #2196f3, #1976d2);
  color: white;
  border-radius: 50rpx;
  padding: 25rpx 40rpx;
  font-size: 28rpx;
  border: none;
  outline: none;
  box-shadow: 0 4rpx 15rpx rgba(33, 150, 243, 0.3);
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 10rpx;
  min-width: 120rpx;
  justify-content: center;
}

.add-btn:active {
  transform: scale(0.95);
  box-shadow: 0 2rpx 10rpx rgba(33, 150, 243, 0.2);
}

.add-icon {
  font-size: 32rpx;
  font-weight: bold;
}

/* 密友列表 */
.friends-section {
  margin-bottom: 30rpx;
}

.friends-section .section-title {
  color: rgba(255, 255, 255, 0.9);
  font-size: 26rpx;
}

.friends-list {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 25rpx;
  overflow: hidden;
  box-shadow: 0 8rpx 30rpx rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(10rpx);
}

/* 空状态提示 */
.empty-tip {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 80rpx 20rpx;
  text-align: center;
}

.empty-icon {
  font-size: 120rpx;
  margin-bottom: 30rpx;
  opacity: 0.5;
}

.empty-text {
  font-size: 28rpx;
  color: #999;
  background: rgba(33, 150, 243, 0.1);
  padding: 15rpx 30rpx;
  border-radius: 25rpx;
}

/* 密友列表项 */
.friend-item {
  display: flex;
  flex-direction: column;
  padding: 30rpx;
  border-bottom: 1rpx solid rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.friend-item:active {
  background: rgba(33, 150, 243, 0.05);
}

.friend-item:last-child {
  border-bottom: none;
}

.friend-info {
  display: flex;
  align-items: center;
  gap: 20rpx;
  margin-bottom: 25rpx;
}

.avatar-container {
  position: relative;
}

.avatar {
  width: 80rpx;
  height: 80rpx;
  border-radius: 50%;
  background: linear-gradient(135deg, #2196f3, #1976d2);
  color: white;
  font-size: 48rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4rpx 20rpx rgba(33, 150, 243, 0.3);
  transition: all 0.3s ease;
}

.avatar:active {
  transform: scale(1.1);
}

.friend-details {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 10rpx;
}

.friend-name {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.friend-id {
  font-size: 24rpx;
  color: #666;
  background: rgba(33, 150, 243, 0.1);
  padding: 5rpx 15rpx;
  border-radius: 15rpx;
  display: inline-block;
  align-self: flex-start;
}

.permission-status {
  display: flex;
  align-items: center;
  gap: 10rpx;
  align-self: flex-start;
}

.permission-dot {
  width: 15rpx;
  height: 15rpx;
  border-radius: 50%;
  background: #ccc;
  transition: all 0.3s ease;
}

.permission-dot.enabled {
  background: #42b983;
  box-shadow: 0 0 10rpx rgba(66, 185, 131, 0.5);
}

.permission-text {
  font-size: 22rpx;
  color: #666;
}

/* 密友操作按钮 */
.friend-actions {
  display: flex;
  gap: 20rpx;
  justify-content: flex-end;
}

.action-btn {
  padding: 15rpx 30rpx;
  border-radius: 50rpx;
  font-size: 26rpx;
  border: none;
  outline: none;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 10rpx;
  min-width: 140rpx;
  justify-content: center;
}

.action-btn:active {
  transform: scale(0.95);
}

.view-btn {
  background: linear-gradient(135deg, #42b983, #35495e);
  color: white;
  box-shadow: 0 4rpx 15rpx rgba(66, 185, 131, 0.3);
}

.view-btn:disabled {
  background: linear-gradient(135deg, #ccc, #999);
  box-shadow: none;
  opacity: 0.7;
}

.delete-btn {
  background: linear-gradient(135deg, #ff6b6b, #ee5a6f);
  color: white;
  box-shadow: 0 4rpx 15rpx rgba(255, 107, 107, 0.3);
}

.btn-icon {
  font-size: 28rpx;
}
</style>